package edu.itla.medifacil.controllers;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.itla.medifacil.helpers.Utilities;
import edu.itla.medifacil.persistence.entities.People;
import edu.itla.medifacil.persistence.entities.Prescription;
import edu.itla.medifacil.persistence.entities.Suffering;
import edu.itla.medifacil.persistence.enums.Role;
import edu.itla.medifacil.persistence.managers.PersonManager;
import edu.itla.medifacil.persistence.managers.PrescriptionManager;
import edu.itla.medifacil.persistence.managers.SufferingManager;

/**
 * Servlet implementation class Precriptions
 */

public class Prescriptions extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private final PrescriptionManager prescriptionManager;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Prescriptions() {
        super();
        prescriptionManager = new PrescriptionManager();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		if( "add".equals( request.getParameter("action") ) ){
			SufferingManager sufferingManager = new SufferingManager();
			List<Suffering> sufferings = sufferingManager.findAll();
			List<People> patients  = new PersonManager().findAll( Role.PATIENT.getId() );
			
			request.getSession(true).setAttribute("sufferings", sufferings);
			request.getSession().setAttribute("patients", patients);
			
			response.sendRedirect("prescriptions/add.jsp");
		}else if( "view".equals( request.getParameter("action") ) ){
			Prescription prescription = prescriptionManager.find( Integer.parseInt( request.getParameter("id") ) );
			request.getSession(true).setAttribute("prescription", prescription);
			
			response.sendRedirect("prescriptions/view.jsp");
			
		}else if( "edit".equals( request.getParameter("action") ) ){
			SufferingManager sufferingManager = new SufferingManager();
			PrescriptionManager prescriptionManager = new PrescriptionManager();
			
			List<Suffering> sufferings = sufferingManager.findAll();
			List<People> patients  = new PersonManager().findAll( Role.PATIENT.getId() );
			Prescription prescription = prescriptionManager.find( Integer.parseInt( request.getParameter("id") ) );
			
			request.getSession(true).setAttribute("sufferings", sufferings);
			request.getSession().setAttribute("patients", patients);
			request.getSession().setAttribute("prescription", prescription);
			
			response.sendRedirect("prescriptions/edit.jsp");
		}else{
			List<Prescription> prescriptions = prescriptionManager.findAll();
			request.getSession(true).setAttribute("prescriptions", prescriptions);
			response.sendRedirect("prescriptions/");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if( "save".equals( request.getParameter("action") ) ){
			Map<String, String> parameters = Utilities.parametersArrayToMap( request.getParameterMap() );
			Prescription prescription = prescriptionManager.save(parameters);
			
			request.getSession().setAttribute("prescription", prescription);
			response.sendRedirect("prescriptions/view.jsp");
		}else if( "addSuffering".equals(request.getParameter("action")) ){
			SufferingManager sufferingManager = new SufferingManager();
			Map<String, String> parameters = Utilities.parametersArrayToMap( request.getParameterMap() );
			sufferingManager.save( parameters );
		}
	}

}
